package com.wutong.erb.other;


import jakarta.servlet.http.Cookie;
import lombok.RequiredArgsConstructor;

import java.util.concurrent.TimeUnit;

/**
 * CookieBuilder
 * @author wutong
 */
public class CookieBuilder {

    private final Cookie cookie;

    public CookieBuilder(String name, String value) {
        this.cookie = new Cookie(name, value);
    }

    public CookieBuilder setPath(String path) {
        cookie.setPath(path);
        return this;
    }

    public CookieBuilder setDomain(String domain) {
        cookie.setDomain(domain);
        return this;
    }

    public CookieBuilder setMaxAge(int maxAge) {
        cookie.setMaxAge(maxAge);
        return this;
    }

    public CookieBuilder setMaxAge(int maxAge, TimeUnit unit) {
        cookie.setMaxAge((int) unit.toSeconds(maxAge));
        return this;
    }

    public CookieBuilder setSecure(boolean secure) {
        cookie.setSecure(secure);
        return this;
    }

    public CookieBuilder setHttpOnly(boolean httpOnly) {
        cookie.setHttpOnly(httpOnly);
        return this;
    }

    public CookieBuilder setAttribute(String name, String value) {
        cookie.setAttribute(name, value);
        return this;
    }

    public CookieBuilder setValue(String newValue) {
        cookie.setValue(newValue);
        return this;
    }

    public Cookie build() {
        return cookie;
    }
}
